package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonGoodslistAsyGet;
import com.zcx.qshop.entity.Good;

import java.util.List;

/**
 * Created by Administrator on 2015/8/23 0023.
 */
public abstract class ThreeListAdapter extends QSAdapter<JsonGoodslistAsyGet.Info.GoodDouble> {

    private LayoutInflater layoutInflater;

    public ThreeListAdapter(Context context, List<JsonGoodslistAsyGet.Info.GoodDouble> objects) {

        super(context, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final JsonGoodslistAsyGet.Info.GoodDouble goodDouble = getItem(position);

        ViewHolder viewHolder;

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_three_list, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()){

                    case R.id.item_three_list_one:

                        onDetail(goodDouble.goodOne);

                        break;

                    case R.id.item_three_list_two:

                        onDetail(goodDouble.goodTwo);

                        break;

                }

            }

        };

        Good good;

        if((good = goodDouble.goodOne) != null){

            viewHolder.one.setVisibility(View.VISIBLE);

            viewHolder.one.setOnClickListener(onClickListener);

            viewHolder.imgOne.setImageURI(Uri.parse(good.picurl));

            viewHolder.contentOne.setText(Html.fromHtml(good.title + " " + good.description));

            viewHolder.priceOne.setText(good.price);

            viewHolder.countOne.setText("销量：" + good.number);

        }else{

            viewHolder.one.setVisibility(View.INVISIBLE);

        }

        if((good = goodDouble.goodTwo) != null){

            viewHolder.two.setVisibility(View.VISIBLE);

            viewHolder.two.setOnClickListener(onClickListener);

            viewHolder.imgTwo.setImageURI(Uri.parse(good.picurl));

            viewHolder.contentTwo.setText(good.title + " " + good.description);

            viewHolder.priceTwo.setText(good.price);

            viewHolder.countTwo.setText("销量：" + good.number);

        }else{

            viewHolder.two.setVisibility(View.INVISIBLE);

        }

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.item_three_list_one) private View one;

        @BoundView(R.id.item_three_list_img_one) private SimpleDraweeView imgOne;

        @BoundView(R.id.item_three_list_content_one) private TextView contentOne;

        @BoundView(R.id.item_three_list_price_one) private TextView priceOne;

        @BoundView(R.id.item_three_list_count_one) private TextView countOne;

        @BoundView(R.id.item_three_list_two) private View  two;

        @BoundView(R.id.item_three_list_img_two) private SimpleDraweeView imgTwo;

        @BoundView(R.id.item_three_list_content_two) private TextView contentTwo;

        @BoundView(R.id.item_three_list_price_two) private TextView priceTwo;

        @BoundView(R.id.item_three_list_count_two) private TextView countTwo;

    }

    protected abstract void onDetail(Good good);

}
